package crafex.engine.sound;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {
	
	//TODO SOUND
	
	private  SoundPool soundPool;
	@SuppressWarnings("rawtypes")
	private  HashMap soundPoolMap;
	private  AudioManager  audioManager;
	private  Context context;
	
	@SuppressWarnings("rawtypes")
	public void initSounds(Context theContext) {
	    context = theContext;
	    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
	    soundPoolMap = new HashMap();
	    audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
	}
	
	@SuppressWarnings("unchecked")
	public void addSound(int index, int SoundID)
	{
	    soundPoolMap.put(index, soundPool.load(context, SoundID, 1));
	}
	
	public void playSound(int index)
	{
	float streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	streamVolume = streamVolume / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	    soundPool.play((Integer) soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
	}
	 
	public void playLoopedSound(int index)
	{
	    float streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	    streamVolume = streamVolume / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	    soundPool.play((Integer) soundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
	}
	
	public void stopSound(int index){
		soundPool.stop((Integer) soundPoolMap.get(index));
	}
}
